package cn.tecnova.cd

import cn.tecnova.utils.ConfigHandler
import org.apache.log4j.{Level, Logger}
import org.apache.spark.sql.{Dataset, SQLContext}
import org.apache.spark.{SparkConf, SparkContext}
import org.elasticsearch.spark.rdd.EsSpark

/**
  * description:
  * 1007 7685
  **/
object TecnovaJudgement {
  Logger.getLogger("org").setLevel(Level.ERROR)

  def main(args: Array[String]): Unit = {

    val conf = new SparkConf()
      .setAppName(this.getClass.getSimpleName)
      //      .setMaster("local[*]")
      .set("es.index.auto.create", "true")
      .set("es.nodes", ConfigHandler.esNodes)
      .set("es.port", ConfigHandler.esPort)
      .set("es.nodes.wan.only", "true")

    val sc = new SparkContext(conf)
    val sqLContext = new SQLContext(sc)

    import sqLContext.implicits._
    val rowDF = sqLContext.read.jdbc("jdbc:mysql://www.slave4.com:3306/tec_judgement", "qichacha_judgement", "id",1,10077685,10,ConfigHandler.props2)

    //导入写es相关的包
    import org.elasticsearch.spark.sql._

    val result: Dataset[TecnovaJudgement] = rowDF.map(row => {
      TecnovaJudgement(
        row.getAs[Long]("id"),
        row.getAs[String]("KeyNo"),
        row.getAs[String]("TID"),
        row.getAs[String]("Court"),
        row.getAs[String]("CaseName"),
        row.getAs[String]("CaseNo"),
        row.getAs[String]("CaseType"),
        row.getAs[String]("SubmitDate"),
        row.getAs[Boolean]("IsProsecutor").toString,
        row.getAs[Boolean]("IsDefendant").toString,
        row.getAs[String]("UpdateDate"),
        row.getAs[String]("CourtYear"),
        row.getAs[String]("CaseRole")

      )
    })

//    result.show(10)
//    result.saveToEs("tecnova_judgement2" + "/company_judgement")

    EsSpark.saveToEs(result.rdd,"tecnova_judgement/company_judgement",Map("es.mapping.id" -> "id"))


    sc.stop()

  }

}

case class TecnovaJudgement(

                            var id:Long,
                            var keyNo:String,
                            var tID:String,
                            var court:String,
                            var caseName:String,
                            var caseNo:String,
                            var caseType:String,
                            var submitDate:String,
                            var isProsecutor:String,
                            var isDefendant:String,
                            var updateDate:String,
                            var courtYear:String,
                            var caseRole:String

                           )
